const { ccclass, property } = cc._decorator;

@ccclass
export class PageIndicator extends cc.PageViewIndicator {
    _changedState = function () {
        var indicators = this._indicators;
        if (indicators.length === 0) return;
        var idx = this._pageView.getCurrentPageIndex();
        if (idx >= indicators.length) return;
        for (var i = 0; i < indicators.length; ++i) {
            var node = indicators[i];
            node.color = cc.color(207, 207, 207);
        }
        indicators[idx].color = cc.color(116, 200, 26);
    }

    _refresh = function () {
        if (!this._pageView) { return; }
        var indicators = this._indicators;
        var len = this._pageView.getTotalCount();
        if (len === indicators.length) {
            return;
        }
        var i = 0;
        if (len > indicators.length) {
            for (i = 0; i < len; ++i) {
                if (!indicators[i]) {
                    indicators[i] = this._createIndicator();
                }
            }
        }
        else {
            var count = indicators.length - len;
            for (i = count; i > 0; --i) {
                var node = indicators[i - 1];
                this.node.removeChild(node);
                indicators.splice(i - 1, 1);
            }
        }
        if (this._layout && this._layout.enabledInHierarchy) {
            this._layout.updateLayout();
        }
        this._changedState();
    }
}